//
//  CDSignatureViewController.swift
//  PDF
//
//  Created by Kellv on 2024/4/21.
//

import UIKit
import SignatureView

class CDSignatureViewController: UIViewController {
    
    @IBOutlet weak var penBtn: UIButton!
    @IBOutlet weak var undoBtn: UIButton!
    @IBOutlet weak var doneBtn: UIButton!
    @IBOutlet weak var cancelBtn: UIButton!
    
    @IBOutlet weak var eraserbtn: UIButton!
    
    @IBOutlet weak var sizeTitleLabel: UILabel!
    
    @IBOutlet weak var siguratureBgView: UIView!
    @IBOutlet weak var colorviews: UICollectionView!
    @IBOutlet weak var sizeValuelabel: UILabel!
    
    @IBOutlet weak var sizeSlider: UISlider!
    private var selectColorIndex = -1
    var signatureView: SignatureView!

    var colors: [ColorType] = [.color1,.color2,.color4,.color5,.color6,.color9,.color10,.color11,.color12]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .baseBgColor
        var transform = CATransform3DIdentity
        transform = CATransform3DRotate(transform, CGFloat.pi / 2, 0, 0, 1)
        self.view.layer.transform = transform
        
        cancelBtn.setTitle("Cannel".localize(), for: .normal)
        cancelBtn.backgroundColor = UIColor(red: 0.255, green: 0.443, blue: 1, alpha: 0.15)
        cancelBtn.setTitleColor(UIColor(red: 0.498, green: 0.537, blue: 0.631, alpha: 1), for: .normal)
        cancelBtn.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 12)
        cancelBtn.layer.cornerRadius = 16

        doneBtn.layer.cornerRadius = 16
        doneBtn.setTitle("Done".localize(), for: .normal)
        doneBtn.setTitleColor(.white, for: .normal)
        doneBtn.backgroundColor = UIColor(red: 0.255, green: 0.443, blue: 1, alpha: 1)
        doneBtn.titleLabel?.font = .helvBold(12)
        
        undoBtn.layer.cornerRadius = 16
        
        sizeTitleLabel.font = .regular(14)
        sizeTitleLabel.textColor = .black
        sizeTitleLabel.text = "Size".localize()
        sizeValuelabel.font = .regular(14)
        sizeValuelabel.textColor = .black
        sizeValuelabel.text = "30"
        
        penBtn.setImage("edit_pen_selected".image, for: .selected)
        penBtn.setImage("edit_pen".image, for: .normal)
        
        eraserbtn.setImage("edit_eraser".image, for: .normal)
        eraserbtn.setImage("edit_eraser_selected".image, for: .selected)
       
        colorviews.delegate = self
        colorviews.dataSource = self
        colorviews.backgroundColor = .baseBgColor
        colorviews.register(CDColorCell.self, forCellWithReuseIdentifier: "CDColorCell")
        colorviews.reloadData()
//        
        signatureView = SignatureView(frame: siguratureBgView.bounds)
        signatureView.backgroundLineColor = .black
        signatureView.foregroundLineColor = .black
        siguratureBgView.addSubview(signatureView)
        signatureView.isUserInteractionEnabled = false
    }

    @IBAction func onCacncelBtn(_ sender: Any) {
        self.dismiss(animated: true)
    }
    
    @IBAction func onDoneAction(_ sender: Any) {
        
    }
    @IBAction func undoOrRedoAction(_ sender: UIButton) {
        if sender.tag == 1 { //redo
//            signatureView.r
        } else {
            
        }
    }
    
    @IBAction func penOrEraserAction(_ sender: UIButton) {
        if sender.tag == 3 { //pen
            penBtn.isSelected = true
            eraserbtn.isSelected = false
            signatureView.isUserInteractionEnabled = true
        } else {
            penBtn.isSelected = false
            eraserbtn.isSelected = true
            signatureView.clear()
        }
    }
    
    @IBAction func onSizeSliderValueChange(_ sender: Any) {
        signatureView.foregroundLineWidth = CGFloat(sizeSlider.value)
        signatureView.backgroundLineWidth = CGFloat(sizeSlider.value)

    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}


extension CDSignatureViewController:UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return colors.count

    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CDColorCell", for: indexPath) as! CDColorCell
        let model = colors[indexPath.item]
        if model == .color1{
            cell.view.layer.borderWidth = 1
            cell.view.layer.borderColor = UIColor(hexStr: "CBCBCB").cgColor
        } else {
            cell.view.layer.borderWidth = 0
        }
        cell.view.backgroundColor = UIColor(hexStr: model.rawValue)
        cell.refeshUI(isSelect: indexPath.item == selectColorIndex)
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectColorIndex = indexPath.item
        let model = colors[indexPath.item]

        collectionView.reloadData()
        signatureView.backgroundLineColor = UIColor(hexStr: model.rawValue)
        signatureView.foregroundLineColor = UIColor(hexStr: model.rawValue)
    }

}
